#include "ContactsTreeStyledItemDelegate.h"

ContactsTreeStyledItemDelegate::ContactsTreeStyledItemDelegate(QWidget *parent)
    : QStyledItemDelegate(parent)
{

}

ContactsTreeStyledItemDelegate::~ContactsTreeStyledItemDelegate()
{

}

/*
DisplayRole			// 昵称
DecorationRole		// 头像
UserRole + 1		// 类型，是否是组名0，好友1，公众号2等标识
UserRole + 2		// 账号(唯一标识)
UserRole + 3		// 备注名
UserRole + 4		// 个性签名
*/
void ContactsTreeStyledItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    QString strDisplay = index.data(Qt::DisplayRole).toString();	// 昵称或分组名
    int nType = index.data(Qt::UserRole + 1).toInt();				// 类型

    painter->save();
    painter->setRenderHint(QPainter::Antialiasing);

    if (nType == 0)
    {
        // 好友分组
        QRect rcBranch(option.rect.left() + 2, option.rect.top() + option.rect.height()/2 - 6, 12, 12);
        if ((option.state & QStyle::State_Open) == QStyle::State_Open)
        {
            // Expand
            if ((option.state & QStyle::State_MouseOver) == QStyle::State_MouseOver ||
                    (option.state & QStyle::State_Selected) == QStyle::State_Selected)
            {
                // Highlight
                QPixmap pixBackground(":/MainPanel/main_group_highlight");
                painter->drawPixmap(option.rect, pixBackground);
                QPixmap pixBranch(":/MainPanel/MainPanel_FolderNode_expandTextureHighlight");
                painter->drawPixmap(rcBranch, pixBranch);
            }
            else
            {
                // Normal
                QPixmap pixBranch(":/MainPanel/MainPanel_FolderNode_expandTexture");
                painter->drawPixmap(rcBranch, pixBranch);
            }
        }
        else
        {
            // Collapse
            if ((option.state & QStyle::State_MouseOver) == QStyle::State_MouseOver ||
                    (option.state & QStyle::State_Selected) == QStyle::State_Selected)
            {
                // Highlight
                QPixmap pixBackground(":/MainPanel/main_group_highlight");
                painter->drawPixmap(option.rect, pixBackground);
                QPixmap pixBranch(":/MainPanel/MainPanel_FolderNode_collapseTextureHighlight");
                painter->drawPixmap(rcBranch, pixBranch);
            }
            else
            {
                // Normal
                QPixmap pixBranch(":/MainPanel/MainPanel_FolderNode_collapseTexture");
                painter->drawPixmap(rcBranch, pixBranch);

            }
        }

        QRect r = option.rect.adjusted(rcBranch.width() + 8, 0, 0, 0);
        painter->drawText(r, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine, strDisplay);
    }
    else
    {
        // 好友
        QRect rcHead(option.rect.left() + 5, option.rect.top() + 5, option.rect.height() - 10, option.rect.height() - 10);

        if ((option.state & QStyle::State_MouseOver) == QStyle::State_MouseOver)
        {
            painter->fillRect(option.rect, QColor(252, 240, 193));
        }
        else if ((option.state & QStyle::State_Selected) == QStyle::State_Selected)
        {
            painter->fillRect(option.rect, QColor(252, 235, 168));
        }


        // 昵称
        QRect r = option.rect.adjusted(rcHead.width() + 10, 0, 0, -option.rect.height() / 2 - 1);
        painter->drawText(r, Qt::AlignBottom | Qt::AlignLeft | Qt::TextSingleLine, strDisplay);

        // 账号
        painter->setPen(Qt::darkGray);
        r = option.rect.adjusted(rcHead.width() + 10, option.rect.height() / 2 + 1, 0, 0);
        painter->drawText(r, Qt::AlignTop | Qt::AlignLeft | Qt::TextSingleLine, strDisplay);


        // 头像
        //QRect rcPixmap(option.rect.left() + 2, option.rect.top() + 2, option.rect.height() - 4, option.rect.height() - 4);
        //QPixmap pixmap = qvariant_cast<QPixmap>(index.data(Qt::DecorationRole));
        QIcon ico = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
        QPainterPath pathFace;
        pathFace.addRoundRect(rcHead, 15, 15);
        painter->setClipPath(pathFace);
        //painter->drawPixmap(rcPixmap, ico);
        ico.paint(painter, rcHead);
        painter->setClipping(false);

        if ((option.state & QStyle::State_Selected) == QStyle::State_Selected)
        {
            painter->setPen(QColor(170, 206, 197));
            painter->drawRoundRect(rcHead, 15, 15);
        }
    }



    painter->restore();

}

QSize ContactsTreeStyledItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    QSize sz = QStyledItemDelegate::sizeHint(option, index);
    int nType = index.data(Qt::UserRole + 1).toInt();				// 类型

    if (nType == 0)
    {
        return QSize(sz.width(), 25);
    }

    return QSize(sz.width(), 50);
    //return QStyledItemDelegate::sizeHint(option, index);
}
